<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\core\config;
use src\logic\lstudio;
use src\logic\lshare;
class Canchor extends controller {
	//返回基本信息
	public function main() {
		$view   = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$userid = (int)input::get('userid');
		//个人基本信息
		$where  = array('userid'=>$userid);
		$info   = C\model::load('pro.pl.userinfo')->get_one($userid);
		$anchor = C\model::load('pro.pl.anchor')->get_one($userid);
		$user   = array();
		$config = C\config::load('config', false);
		$user['nickname'] = $info['nickname'];
		$user['sex']    = $info['sex'];
		$user['birth']  = $info['birth'];
		$user['email']  = $info['email'];
		$user['mobile'] = $info['mobile'];
		$user['city']   = $info['city'];
		$user['lv']     = $info['lv'];
		$user['thumb']  = $config['website'].$info['thumb'];
		$user['xfans']  = $anchor['xfans'];
		$user['charms'] = $anchor['charms'];		
		//个人签约录入信息
		$sign   = C\model::load('pro.sign.basic')->get_one($userid);
		$sign['starlv'] = '';
		if ($anchor['starlv'] > 0) {
			$star   = C\model::load('pro.inc.star')->get_one($anchor['starlv']);
			$sign['starlv'] = $star['name'].'['.$star['star'].']';
		}
		$sign['nextstarlv'] = '';
		if ($anchor['nextstarlv'] > 0) {
			$star   = C\model::load('pro.inc.star')->get_one($anchor['nextstarlv']);
			$sign['nextstarlv'] = $star['name'].'['.$star['star'].']';
		}
		$sign['signtime'] = date('Y-m-d', $sign['signtime']);
		$sign['lasttime'] = date('Y-m-d', $sign['lasttime']);
		$sign['stime']    = date('Y-m-d H:i:s', $sign['stime']);
		$tags   = explode(',', $sign['category']);
		$orm    = C\model::load('pro.inc.tags');
		$tag    = array();
		foreach ($tags as $tagid) {
			$one = $orm->get_one($tagid);
			$tag[] = $one['name'];
		}
		$sign['category'] = $tag ? implode(',', $tag) : '';
		$sign['jointime'] = date('Y-m-d H:i:s', $anchor['jointime']);
		//获取第三方粉丝数总和
		$table  = C\model::load('pro.sign.sns')->get_table();
		$orm    = C\model::load('pro.sign.sns')->get_orm();
		$sql    = "select sum(xfans) from " . $table .' where userid=' . $userid;
		$xfans  = $orm->get_value($sql);
		$dlang  = C\lang::inst('dd');
		$view->assign_by_ref('xfans', $xfans);
		$view->assign_by_ref('userid', $userid);
		$view->assign_by_ref('user', $user);
		$view->assign_by_ref('sign', $sign);
		$view->assign_by_ref('optype', $dlang->get('sign_type'));
		$view->assign_by_ref('status', $dlang->get('sh0005'));
		$view->assign_by_ref('sex', $dlang->get('sh0003'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/anchor.ext.js');
	}
	
	//查询公式
	public function doquery() {
		$userid = (int)input::get('userid');
		$anchor = C\model::load('pro.pl.anchor')->get_one($userid);
		$star   = C\model::load('pro.inc.star')->get_one($anchor['starlv']);
		$salary = '';
		if ($star['salary']) {
			$arr = json_decode($star['salary'], 1);
			foreach ($arr as $val) {
				$salary .= '>='.$val['hour'].': '.$val['salary']."canadian dollar<br/>";
			}
		}
		ob_end_clean();
		echo $salary;
	}

	//签约录入
	public function dosign() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$userid = (int)input::post('userid');
		$data   = array();
		$data['skype']    = input::post('skype');
		$starlv = (int)input::post('starlv');
		$nextstarlv = (int)input::post('nextstarlv');
		$data['signtime'] = strtotime(input::post('signtime'));
		$data['lasttime'] = strtotime(input::post('lasttime'));
		$data['executor'] = input::post('executor');
		$data['category'] = input::post('category');
		$data['remark']   = input::post('remark');
		$data['website']  = input::post('website');
		$data['name']     = input::post('name');
		$data['address']  = input::post('address');
		$adminid = Cadmin::islogin($view);
		$admin   = C\model::load('main.sys.user')->get_one($adminid);
		$opuser  = $admin['nickname'].'['.$admin['id'].']';
		$data['operator'] = $opuser;
		$data['stime']    = C\config::$nowtime;
		if ($userid < 1) {
			$view->assign_error('1', 'anchor info not exists!');
		}
		$aorm   = C\model::load('pro.pl.anchor');
		$anchor = $aorm->get_one($userid);
		if (!$anchor) {
			$view->assign_error('1', 'anchor info not exists!');
		}
		$one    = C\model::load('pro.sign.basic')->get_one($userid);
		if ($one) {
			C\model::load('pro.sign.basic')->save($userid, $data);
		} else {
			$data['userid'] = $userid;
			C\model::load('pro.sign.basic')->new_one($data);
		}
		$star = 0;
		if ($starlv > 0)  {
			$res   = lstudio::updateGrade($userid, $starlv);
			if (!$res) {
				$view->assign_error('2', 'The current cycle of the host broadcast time is not zero, cannot be modified');
			}
			$star = C\model::load('pro.inc.star')->get_one($starlv);
			$star = $star['name'].'['.$star['star'].']';
			$field = array('type'=>4, 'request_uri'=>$_SERVER['REQUEST_URI'], 'user'=>$opuser, 
					'msg'=>'Star LV userid:'.$userid.' level:'.$star, 'ip'=>C\get_ip(), 'addtime'=>C\config::$nowtime);
			C\model::load('main.sys.syslog')->new_one($field);
		}
		//设置下一个等级资料信息
		if ($nextstarlv > 0) {
			if (in_array($anchor['starlv'], array(1, 4))) {
				$view->assign_error('3', 'C/S star lv anchor not need to set next star lv.');
			}
			$aorm->save($userid, array('nextstarlv'=>$nextstarlv));
			$nexrstar = C\model::load('pro.inc.star')->get_one($nextstarlv);
			$nexrstar = $nexrstar['name'].'['.$nexrstar['star'].']';
			$field = array('type'=>4, 'request_uri'=>$_SERVER['REQUEST_URI'], 'user'=>$opuser,
					'msg'=>'Next Star LV userid:'.$userid.' level:'.$nexrstar, 'ip'=>C\get_ip(), 'addtime'=>C\config::$nowtime);
			C\model::load('main.sys.syslog')->new_one($field);
		}
		$view->assign_by_ref('starlv', $star);
		$view->display();
	}
	
	//添加sns
	public function savesns() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::post('id');
		$data  = array();
		$data['userid'] = (int)input::get_post('userid');
		$data['name']   = input::post('name');
		$data['xfans']  = input::post('xfans');
		$data['site']   = input::post('site');
		$data['stime']  = C\config::$nowtime;
		$orm = C\model::load('pro.sign.sns');
		if ($id > 0) {
			$orm->save($id, $data);
		} else {
			$orm->new_one($data);
		}
		$view->display();
	}
	
	//sns列表
	public function sns() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid= (int)input::get_post('userid');
		if(!$userid) {
			$view->display();
		}
		$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		$orm    = C\model::load('pro.sign.sns');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//删除sns
	public function delsns() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要删除的记录');
		}
		$orm = C\model::load('pro.sign.sns');
		foreach ($ids as $id) {
			$orm->delete($id);
		}
		$view->display();
	}
	
	//个人底薪记录
	public function salary() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid= (int)input::get_post('userid');
		if(!$userid) {
			$view->display();
		}
		$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		$orm    = C\model::load('pro.pl.salary');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$star = json_decode($item['star'], true);
				$item['star']      = $star['name'].'['.$star['star'].']';
				$item['cycle']     = date('Y-m-d', $item['starttime']).'~'.date('Y-m-d', $item['endtime']);
				$item['addtime']   = date('Y-m-d', $item['addtime']);
				$item['stime']     = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//添加奖金
	public function addbonus() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$data  = array();
		$id    = (int)input::get_post('id');
		$data['userid'] = (int)input::get_post('userid');
		$data['optype'] = (int)input::post('optype');
		$sendtime 		= input::post('sendtime');
		if ($data['optype'] == 1) {
			$anchor   = C\model::load('pro.pl.anchor')->get_one($data['userid']);
			if ($anchor['starlv'] == 1 || $anchor['starlv'] == 4) {
				$view->assign_error(3, 'The anchor level is the basis of C and S can not add bonus');
			}
			if (is_numeric($sendtime)) {
				$where = array('userid'=>$data['userid'], 'cycle'=>$sendtime);
				$item  = C\model::load('pro.pl.bonus')->get_item($where);
				if ($item) {
					$view->assign_error(1, 'This user has been added this month');
				}
				$data['cycle']= $sendtime;
				$anchor   = C\model::load('pro.pl.anchor')->get_one($data['userid']);
				$sendtime = $anchor['jointime'] + $sendtime * 24 * 3600 * 30;
				$data['sendtime'] = $sendtime;
			}
		} else {
			$sendtime = strtotime($sendtime);
			if ($sendtime < C\config::$nowtime) {
				$view->assign_error(2, 'To choose more than the present time');
			}
			$data['sendtime'] = $sendtime;
		}
		$data['money']    = input::post('money');
		$data['coin']     = $data['money'] * lshare::STARRATIO;
		$data['memo']     = input::post('memo');
		$adminid = Cadmin::islogin($view);
		$admin   = C\model::load('main.sys.user')->get_one($adminid);
		$data['operator'] = $admin['nickname'].'['.$admin['id'].']';
		$data['ip']       = C\get_ip();
		$data['stime']    = C\config::$nowtime;
		$orm = C\model::load('pro.pl.bonus');
		if ($id) {
			$orm->save($id, $data);
		} else {
			$orm->new_one($data);
		}
		$view->display();
	}
	
	//奖金列表
	public function bonus() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid= (int)input::get_post('userid');
		if(!$userid) {
			$view->display();
		}
		$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		$orm    = C\model::load('pro.pl.bonus');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['sendtime'] = date('Y-m-d', $item['sendtime']);
				$item['stime']    = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//删除奖金
	public function delbonus() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要删除的记录');
		}
		$orm  = C\model::load('pro.pl.bonus');
		foreach ($ids as $id) {
			$bon = $orm->get_one($id);
			if ($bon['optype'] == 1 && $bon['issend'] == 1) {
				continue;
			}
			if ($bon['optype'] == 2 && $bon['status'] == 1) {
				continue;
			}
			$orm->delete($id);
		}
		$view->display();
	}
	
	//添加评价
	public function addevaluate() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$data  = array();
		$data['userid']  = (int)input::get_post('userid');
		$data['lastaudit'] = input::post('lastaudit');
		$data['lastaudit'] = strtotime($data['lastaudit']);
		if ($data['lastaudit'] > C\config::$nowtime) {
			$view->assign_error(1, 'Want to choose less than the time of now');
		}
		$data['quality'] = input::post('quality');
		$data['issue']   = input::post('issue');
		$data['notes']   = input::post('notes');
		$data['stime']   = C\config::$nowtime;
		$adminid = Cadmin::islogin($view);
		$admin   = C\model::load('main.sys.user')->get_one($adminid);
		$data['operator'] = $admin['nickname'].'['.$admin['id'].']';
		C\model::load('pro.sign.resume')->new_one($data);
		$view->display();
	}
	
	//评价列表
	public function evaluate() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid= (int)input::get_post('userid');
		if(!$userid) {
			$view->display();
		}
		$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		$orm    = C\model::load('pro.sign.resume');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['lastaudit'] = date('Y-m-d', $item['lastaudit']);
				$item['stime']     = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//添加线下关系维护
	public function addrelation() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$data  = array();
		$data['userid']   = (int)input::get_post('userid');
		$data['addtime']  = input::post('addtime');
		$data['addtime']  = strtotime($data['addtime']);
		$data['notes']    = input::post('notes');
		$data['result']   = input::post('result');
		$data['decision'] = input::post('decision');
		$data['executor'] = input::post('executor');
		$adminid = Cadmin::islogin($view);
		$admin   = C\model::load('main.sys.user')->get_one($adminid);
		$data['operator'] = $admin['nickname'].'['.$admin['id'].']';
		$data['ip']       = C\get_ip();
		$data['stime']    = C\config::$nowtime;
		C\model::load('pro.sign.relationship')->new_one($data);
		$view->display();
	}
	
	//线下关系维护列表
	public function relation() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid= (int)input::get_post('userid');
		if(!$userid) {
			$view->display();
		}
		$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		$orm    = C\model::load('pro.sign.relationship');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['addtime'] = date('Y-m-d', $item['addtime']);
				$item['stime']   = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}